package com.nutiteq.formmapper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.nutiteq.MapItem;
import com.nutiteq.components.OnMapElement;
import com.nutiteq.components.Place;
import com.nutiteq.components.WgsPoint;
import com.nutiteq.controls.ControlKeys;
import com.nutiteq.controls.OnScreenZoomControls;
import com.nutiteq.listeners.MapListener;
import com.nutiteq.listeners.OnMapElementListener;
import com.nutiteq.log.Log;
import com.nutiteq.mapper.forms.ChangeMapList;
import com.nutiteq.mapper.forms.GoToForm;
import com.nutiteq.maps.CloudMade;
import com.nutiteq.ui.DefaultCursor;
import com.nutiteq.utils.LogForm;
import com.nutiteq.utils.Utils;

public class FormMapper extends MIDlet implements MapListener, CommandListener,OnMapElementListener {
  private Form form;
  private MapItem mapItem;
  private Image[] icons;
  private static final int ICON_SIZE = 14;
  private final Command exit = new Command("Exit", Command.EXIT, 0);
  private final Command zoomIn = new Command("Zoom in", Command.ITEM, 0);
  private final Command zoomOut = new Command("Zoom out", Command.ITEM, 0);

  private StringItem coords;
  private final FormMapper instance;

  public FormMapper() {
    instance = this;
  }

  protected void pauseApp() {

  }

  protected void startApp() throws MIDletStateChangeException {
    Log.enableAll();
    final Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        try {
          final HttpConnection c = (HttpConnection) Connector.open("http://www.google.com",
              Connector.READ_WRITE);
          final InputStream is = c.openInputStream();
          c.getLength();
        } catch (final IOException ignore) {
        }
        instance.initialize();
      }
    }, 1);
  }

  public void initialize() {
    if (form == null) {
      form = new Form("Mapping form");
      mapItem = new MapItem("Map", "abcdtrial", "Nutiteq","Test", 150, 150, new WgsPoint(24.764580, 59.437420),
          12);

      mapItem.defineControlKey(ControlKeys.MOVE_UP_KEY, Canvas.KEY_NUM2);
      mapItem.defineControlKey(ControlKeys.MOVE_DOWN_KEY, Canvas.KEY_NUM8);
      mapItem.defineControlKey(ControlKeys.MOVE_LEFT_KEY, Canvas.KEY_NUM4);
      mapItem.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, Canvas.KEY_NUM6);
      mapItem.defineControlKey(ControlKeys.SELECT_KEY, -5);
   
      mapItem.setCursor(new DefaultCursor(0xFFFF0000));

      mapItem.showZoomLevelIndicator(true);
      mapItem.startMapping();
      mapItem.setMapListener(this);
      mapItem.setOnScreenZoomControls(new OnScreenZoomControls(Utils
    	        .createImage(OnScreenZoomControls.DEFAULT_ZOOM_IMAGE)));
      mapItem.setCursor(new DefaultCursor(0xFFFF0000));
      
      try {
        final Image tmp = Image.createImage("/icons.png");
        icons = new Image[tmp.getWidth() / ICON_SIZE];
        for (int x = 0; x < tmp.getWidth(); x += ICON_SIZE) {
          icons[x / ICON_SIZE] = Image.createImage(tmp, x, 0, ICON_SIZE, ICON_SIZE,
              Sprite.TRANS_NONE);
        }
      } catch (final IOException e) {
        e.printStackTrace();
      }

      mapItem.addPlace(new Place(1, "Tallinn", icons[0], 24.764580, 59.437420));
      coords = new StringItem("Event: ", "");

      form.append(mapItem);
      form.append(coords);
      form.addCommand(zoomIn);
      form.addCommand(zoomOut);
      form.addCommand(exit);
      form.addCommand(GoToForm.GO_TO_COMMAND);
      //#if !release.build
      form.addCommand(ChangeMapList.CHANGE_MAP_COMMAND);
      //#endif
      form.addCommand(LogForm.SHOW_LOG);
      form.setCommandListener(this);
    }

    Display.getDisplay(this).setCurrent(form);
  }

  protected void destroyApp(final boolean arg0) throws MIDletStateChangeException {
    form = null;
    notifyDestroyed();
  }

  public void mapClicked(final WgsPoint p) {
    coords.setText("clicked " + p.getLon() + " : " + p.getLat());
  }

  public void mapMoved() {

    final WgsPoint center = mapItem.getMiddlePoint();
    coords.setText("moved to " + center.toString());

  }

  public void needRepaint(final boolean mapIsComplete) {
    System.out.println("needRepaint(" + mapIsComplete + ")");
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == exit) {
      mapItem.stopMapping();
      try {
        destroyApp(true);
      } catch (final MIDletStateChangeException ignore) {
      }
    } else if (cmd == zoomIn) {
      mapItem.zoomIn();
    } else if (cmd == zoomOut) {
      mapItem.zoomOut();
    } else if (cmd == GoToForm.GO_TO_COMMAND) {
      Display.getDisplay(this).setCurrent(new GoToForm(this, form, mapItem));
    } else if (cmd == LogForm.SHOW_LOG) {
      Display.getDisplay(this).setCurrent(new LogForm(mapItem, this, form));
    }
    //#if !release.build
    else if (cmd == ChangeMapList.CHANGE_MAP_COMMAND) {
      Display.getDisplay(this).setCurrent(new ChangeMapList(this, form, mapItem));
    }
    //#endif
  }

public void elementClicked(OnMapElement element) {
	 coords.setText("Clicked  on " + element.getLabel());
}

public void elementEntered(OnMapElement element) {
	coords.setText("Entered " + element.getLabel());
	
}

public void elementLeft(OnMapElement element) {
	coords.setText("Left " + element.getLabel());
	
}
}
